/*******************************************************************************
 * Licensed Materials - Property of IBM
 * (c) Copyright IBM Corporation 2007. All Rights Reserved.
 * 
 * Note to U.S. Government Users Restricted Rights:
 * Use, duplication or disclosure restricted by GSA ADP Schedule
 * Contract with IBM Corp. 
 *******************************************************************************/
package com.example.jazztalk.rcp.ui.actions;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;

import com.example.jazztalk.client.IJazzTalkClientLibrary;
import example.jazztalk.common.model.Message;
import com.ibm.team.repository.client.IItemManager;
import com.ibm.team.repository.common.IContributor;
import com.ibm.team.repository.common.TeamRepositoryException;

public class ShowJazzTalkMessagesAction extends AbstractJazzTalkAction {
	private String _messageBuffer;

	protected void callService(IProgressMonitor monitor)
			throws TeamRepositoryException {
		IJazzTalkClientLibrary clientLibrary = getClientLibrary();
		IItemManager itemManager = getRepository().itemManager();

		Message[] messages = clientLibrary.getAllMessages(monitor);

		StringBuilder buffer = new StringBuilder();
		for (Message message : messages) {
			IContributor contributor = (IContributor) itemManager
					.fetchCompleteItem(message.getModifiedBy(),
							IItemManager.DEFAULT, monitor);

			buffer.append(contributor.getName());
			buffer.append(" said: ");
			buffer.append(message.getText());
			buffer.append("\n");
		}

		_messageBuffer = buffer.toString();
	}

	protected void showResults() {
		MessageDialog.openInformation(getShell(), "JazzMail Messages",
				_messageBuffer);
	}
}
